# $Id :  $



# Copyright 2008-2010, Pascal Fautrero & JCharles
#
# This file is part of PyArchiver.
# 
# PyArchiver is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# PyArchiver is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with PyArchiver.  If not, see <http://www.gnu.org/licenses/>.



# Import packages

import os.path
import MySQLdb

#---------------------------------------------------------------------------

    # ==========================================================
    #
    #			PyArchiver - SQL module
    #	
    # ==========================================================

class DataBaseObject():
    def __init__(self, dbName, tableName):
        self.dbName = dbName
        self.tableName = tableName
        self.SQLConnexion = self.Connect2Server()

        if self.SQLConnexion:
            self.Connect2DataBase()

			
    # ==========================================================
    #
    #			Connect to the MySQL server
    #
    # ==========================================================

    def Connect2Server(self):

        try:
            self.conn = MySQLdb.connect (host = "localhost", user = "root", passwd = "")
            self.MyCursor = self.conn.cursor()
            return 1
        except MySQLdb.Error, e:
            return 0


    # ==========================================================
    #
    #			Connect to the database
    #
    # ==========================================================

    def Connect2DataBase(self):    

        try:
            self.conn.select_db('%s' %self.dbName)
        except:
            self.MyCursor.execute ("CREATE DATABASE %s" %self.dbName)
            self.conn.select_db('%s' %self.dbName)
            self.MyCursor.execute ("CREATE TABLE %s(dossiers CHAR(40),supports CHAR(40))" %self.tableName)

			
    # ==========================================================
    #
    #			Disconnect to the server
    #
    # ==========================================================

    def Disconnect2Server():
        self.MyCursor.close ()
        self.conn.commit ()
        self.conn.close ()

		
    # ==========================================================
    #
    #		    Display dossiers in ListControl
    #
    # ==========================================================

    def RetrieveRecords(self,fieldName):
        SQLRequest = "SELECT %s FROM %s GROUP BY %s ORDER BY %s ASC" %(fieldName, self.tableName, fieldName, fieldName)
        self.MyCursor.execute(SQLRequest)
        return self.MyCursor.fetchall()
       
        
    # ==========================================================
    #
    #	 Display dossiers in ListControl filtered by support
    #
    # ==========================================================

    def FilterField(self, fieldSelector, itemSelector, fieldFiltered):
        SQLRequest = "SELECT %s FROM %s WHERE %s='%s' GROUP BY %s ORDER BY %s ASC " %(fieldFiltered, self.tableName,fieldSelector, itemSelector, fieldFiltered, fieldFiltered)    
        self.MyCursor.execute (SQLRequest)
        return self.MyCursor.fetchall()        
            

    # ==========================================================
    #
    #	   Delete records containing the specified support
    #
    # ==========================================================

    def DeleteSupport(self,TheSupport):
        self.MyCursor.execute ("DELETE FROM %s WHERE supports='%s'" %(self.tableName,TheSupport))


    # ==========================================================
    #
    #	   Delete records containing the specified support
    #
    # ==========================================================

    def DeleteFolder(self,TheFolder):
        self.MyCursor.execute ("DELETE FROM %s WHERE dossiers='%s'" %(self.tableName,TheFolder))


    # ==========================================================
    #
    #		     Fill the table PyArchiver
    #
    # ==========================================================

    def FillDataBase(self,drivename, FileList, ThisSupport):

        if ThisSupport != "":
            for f in FileList:
                if os.path.isdir(os.path.join(drivename, f)):
                    if len(ThisSupport) > 40:
                        ThisSupport = ThisSupport[0:40]
                    self.MyCursor.execute ("INSERT INTO %s SET dossiers='%s', supports='%s'" %(self.tableName,f, ThisSupport))


